﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibarayMS
{
    public partial class Frm_return_manage : Form
    {
        private string mname,mid;
        public event myDelegate returnEvent;
        public event myDelegate logoutEvent;
        public Frm_return_manage(string mname,string mid)
        {
            this.mname = mname;
            this.mid = mid;
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            returnEvent();
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            logoutEvent();
            this.Close();
        }

        private void Frm_return_manage_Load(object sender, EventArgs e)
        {
            lbl_mname.Text = mname;
            cb_head.SelectedIndex = 0;
            bind_return_info();
        }

        private void bind_return_info()
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = "SELECT pay,RID,SID,ISBN,BName,author,EName,edit_ID,borrow_time,return_time,repayment_time,renew_num FROM fine AS a RIGHT JOIN (" +
                "select a.RRID,c.SRID,RID,SID,ISBN,BName,author,EName,edit_ID,borrow_time,return_time,repayment_time,renew_num from reader_info a,book_info b,book_store_info c,book_borrow d,book_return e ,book_edit_info f,edit_house_info g,book_date_info h " +
                "where a.RRID=d.RRID and b.BRID=c.BRID and c.SRID=d.SRID and c.SRID=e.SRID and d.RRID=e.RRID and d.SRID=e.SRID and b.BRID=f.BRID and f.PHID=g.PHID and b.BRID=h.BRID) AS b ON a.RRID=b.RRID AND a.SRID=b.SRID;";
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["RID"].ToString());
                myitem.SubItems.Add(dr["SID"].ToString());
                myitem.SubItems.Add(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                myitem.SubItems.Add(dr["author"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["edit_ID"].ToString());
                myitem.SubItems.Add(Convert.ToDateTime(dr["borrow_time"]).ToString("yyyy-MM-dd"));
                myitem.SubItems.Add(Convert.ToDateTime(dr["repayment_time"]).ToString("yyyy-MM-dd"));
                if (dr["return_time"].ToString()=="")
                {
                    myitem.SubItems.Add("");
                }
                else
                {
                    myitem.SubItems.Add(Convert.ToDateTime(dr["return_time"]).ToString("yyyy-MM-dd"));
                }
                if (dr["pay"].ToString() == "True")
                {
                    //已缴费
                    myitem.SubItems.Add("已缴费");
                }
                else
                {
                    if (DateTime.Compare((Convert.ToDateTime(dr["repayment_time"])), DateTime.Now) < 0)
                    {
                        myitem.SubItems.Add("已超期");
                    }
                    else
                    {
                        myitem.SubItems.Add("未超期");
                    }
                }
                myitem.SubItems.Add(dr["renew_num"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count > 0)
            {
                ListViewItem myitem = listView1.SelectedItems[0];
                txt_rid.Text = myitem.SubItems[0].Text;
                if (cb_head.SelectedIndex == 0) { txt_book.Text = myitem.SubItems[2].Text; }
                else if (cb_head.SelectedIndex == 1) { txt_book.Text = myitem.SubItems[3].Text; }
                else { txt_book.Text = myitem.SubItems[1].Text; }
            }
        }

        private void cb_head_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count>0)
            {
                ListViewItem myitem = listView1.SelectedItems[0];
                if (cb_head.SelectedIndex == 0) { txt_book.Text = myitem.SubItems[3].Text; }
                else if (cb_head.SelectedIndex == 1) { txt_book.Text = myitem.SubItems[2].Text; }
                else { txt_book.Text = myitem.SubItems[1].Text; }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            clearTextBox();
            lbl_note.Text = string.Empty;
            bind_return_info();
        }

        private void clearTextBox()
        {
            txt_book.Text = string.Empty;
            txt_rid.Text = string.Empty;
            cb_head.SelectedIndex= 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string rid=txt_rid.Text.Trim();
            string isbn = "", sid = "", bname = "";
            if(cb_head.SelectedIndex == 0) { bname = txt_book.Text.Trim(); }
            else if(cb_head.SelectedIndex == 1) { isbn= txt_book.Text.Trim();}
            else { sid=txt_book.Text.Trim();}
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = string.Format(" SELECT pay, RID, SID, ISBN, BName, author, EName, edit_ID, borrow_time, return_time, repayment_time, renew_num FROM fine AS a " +
                "RIGHT JOIN(select a.RRID,c.SRID,RID,SID,ISBN,BName,author,EName,edit_ID,borrow_time,return_time,repayment_time,renew_num from reader_info a,book_info b,book_store_info c,book_borrow d,book_return e ,book_edit_info f,edit_house_info g,book_date_info h" +
                " where a.RRID=d.RRID and b.BRID=c.BRID and c.SRID=d.SRID and c.SRID=e.SRID and d.RRID=e.RRID and d.SRID=e.SRID and b.BRID=f.BRID and f.PHID=g.PHID and b.BRID=h.BRID and RID like '%{0}%' and BName like '%{1}%' and ISBN like '%{2}%' and SID like'%{3}%') AS b " +
                "ON a.RRID=b.RRID AND a.SRID=b.SRID;", rid, bname, isbn, sid);
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["RID"].ToString());
                myitem.SubItems.Add(dr["SID"].ToString());
                myitem.SubItems.Add(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                myitem.SubItems.Add(dr["author"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["edit_ID"].ToString());
                myitem.SubItems.Add(Convert.ToDateTime(dr["borrow_time"]).ToString("yyyy-MM-dd"));
                myitem.SubItems.Add(Convert.ToDateTime(dr["repayment_time"]).ToString("yyyy-MM-dd"));
                if (dr["return_time"].ToString() == "")
                {
                    myitem.SubItems.Add("");
                }
                else
                {
                    myitem.SubItems.Add(Convert.ToDateTime(dr["return_time"]).ToString("yyyy-MM-dd"));
                }
                if (dr["pay"].ToString() == "True")
                {
                    //已缴费
                    myitem.SubItems.Add("已缴费");
                }
                else
                {
                    if (DateTime.Compare((Convert.ToDateTime(dr["repayment_time"])), DateTime.Now) < 0)
                    {
                        myitem.SubItems.Add("已超期");
                    }
                    else
                    {
                        myitem.SubItems.Add("未超期");
                    }
                }
                myitem.SubItems.Add(dr["renew_num"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要归还的书籍！");
            }
            else
            {
                DialogResult dResult = MessageBox.Show("确定要归还选择的书籍？", "归还提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dResult == DialogResult.Yes)
                {
                    if (listView1.SelectedItems[0].SubItems[10].Text == "已超期")
                    {
                        lbl_note.Text = "已超期！缴费后方可归还";
                        lbl_note.ForeColor = Color.Red;
                        MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                        conn.Open();
                        MySqlCommand cmd = new MySqlCommand("updateBorrow", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        string rid = listView1.SelectedItems[0].SubItems[0].Text;
                        string sid = listView1.SelectedItems[0].SubItems[1].Text;
                        //定义输入函数
                        cmd.Parameters.AddWithValue("rid_", rid);
                        cmd.Parameters.AddWithValue("sid_", sid);
                        cmd.Parameters.AddWithValue("mid_", mid);
                        cmd.Parameters.AddWithValue("defaultType_", 0);
                        //定义输出函数
                        MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                        result.Direction = ParameterDirection.Output;
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        DialogResult dResult1 = MessageBox.Show("是否前往缴费页面？", "系统提示", MessageBoxButtons.YesNo);
                        if (dResult1 == DialogResult.Yes)
                        {
                            Frm_fine_manage frm = new Frm_fine_manage(mname,mid);
                            frm.returnEvent += new myDelegate(Frm_returnEvent);
                            frm.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                        conn.Open();
                        MySqlCommand cmd = new MySqlCommand("returnBook", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        string rid = listView1.SelectedItems[0].SubItems[0].Text;
                        string sid = listView1.SelectedItems[0].SubItems[1].Text;
                        //定义输入函数
                        cmd.Parameters.AddWithValue("rid_", rid);
                        cmd.Parameters.AddWithValue("sid_", sid);
                        cmd.Parameters.AddWithValue("mid_", mid);
                        //0--图书超期
                        cmd.Parameters.AddWithValue("defaultType_", 0);
                        //定义输出函数
                        MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                        result.Direction = ParameterDirection.Output;
                        cmd.ExecuteNonQuery();
                        if (result.Value.ToString() == "1")
                        {
                            lbl_note.Text = "恭喜你，归还成功！";
                            lbl_note.ForeColor = Color.Blue;
                            //清空输入框
                            clearTextBox();
                            bind_return_info();
                        }
                        else
                        {
                            lbl_note.Text = "对不起，归还失败！";
                            lbl_note.ForeColor = Color.Red;
                        }
                        conn.Close();
                    }
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult dResult = MessageBox.Show("确定书籍损坏？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dResult == DialogResult.Yes)
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("updateBorrow", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                string rid = listView1.SelectedItems[0].SubItems[0].Text;
                string sid = listView1.SelectedItems[0].SubItems[1].Text;
                //定义输入函数
                cmd.Parameters.AddWithValue("rid_", rid);
                cmd.Parameters.AddWithValue("sid_", sid);
                cmd.Parameters.AddWithValue("mid_", mid);
                //0--图书超期
                cmd.Parameters.AddWithValue("defaultType_", 1);
                //定义输出函数
                MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                result.Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                conn.Close();
                DialogResult dResult1 = MessageBox.Show("是否前往缴费页面？", "系统提示", MessageBoxButtons.YesNo);
                if (dResult1 == DialogResult.Yes)
                {
                    Frm_fine_manage frm = new Frm_fine_manage(mname, mid);
                    frm.returnEvent += new myDelegate(Frm_returnEvent);
                    frm.Show();
                    this.Hide();
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要续借的书籍！");
            }
            else
            {
                DialogResult dResult = MessageBox.Show("确定要续借选择的书籍？", "续借提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dResult == DialogResult.Yes)
                {
                    if (listView1.SelectedItems[0].SubItems[10].Text == "已超期")
                    {
                        lbl_note.Text = "已超期！缴费后方可续借";
                        lbl_note.ForeColor = Color.Red;
                        MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                        conn.Open();
                        MySqlCommand cmd = new MySqlCommand("updateBorrow", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        string rid = listView1.SelectedItems[0].SubItems[0].Text;
                        string sid = listView1.SelectedItems[0].SubItems[1].Text;
                        //定义输入函数
                        cmd.Parameters.AddWithValue("rid_", rid);
                        cmd.Parameters.AddWithValue("sid_", sid);
                        cmd.Parameters.AddWithValue("mid_", mid);
                        //0--图书超期
                        cmd.Parameters.AddWithValue("defaultType_", 0);
                        //定义输出函数
                        MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                        result.Direction = ParameterDirection.Output;
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        DialogResult dResult1=MessageBox.Show("是否前往缴费页面？", "系统提示", MessageBoxButtons.YesNo);
                        if (dResult1 == DialogResult.Yes)
                        {
                            Frm_fine_manage frm = new Frm_fine_manage(mname, mid);
                            frm.returnEvent += new myDelegate(Frm_returnEvent);
                            frm.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                        conn.Open();
                        MySqlCommand cmd = new MySqlCommand("renewBook", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        string rid = listView1.SelectedItems[0].SubItems[0].Text;
                        string sid = listView1.SelectedItems[0].SubItems[1].Text;
                        //定义输入函数
                        cmd.Parameters.AddWithValue("rid_", rid);
                        cmd.Parameters.AddWithValue("sid_", sid);
                        cmd.Parameters.AddWithValue("mid_", mid);
                        //定义输出函数
                        MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                        result.Direction = ParameterDirection.Output;
                        cmd.ExecuteNonQuery();
                        if (result.Value.ToString() == "1")
                        {
                            lbl_note.Text = "恭喜你，续借成功！";
                            lbl_note.ForeColor = Color.Blue;
                            //清空输入框
                            clearTextBox();
                            bind_return_info();
                        }
                        else
                        {
                            lbl_note.Text = "对不起，续借失败！";
                            lbl_note.ForeColor = Color.Red;
                        }
                        conn.Close();
                    }
                }
            }
        }

        private void Frm_returnEvent()
        {
            this.Show();
            bind_return_info();
        }
    }
}
